

import 'package:bot_toast/bot_toast.dart';
import 'package:dolqun_movies/Entities/VipInformationResult.dart';
import 'package:dolqun_movies/Interface/Network.dart';
import 'package:dolqun_movies/StateManagement/ModelProvider.dart';
import 'package:dolqun_movies/UI/Pages/Models/MainModel.dart';
import 'package:dolqun_movies/UI/Pages/Models/WechatApiModel.dart';
import 'package:dolqun_movies/UI/Widgets/DolqunAppBar.dart';
import 'package:dolqun_movies/UI/Widgets/EnsureHasSignin.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class BuyVipPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    var gridTextStyle = TextStyle(inherit: true, color: Colors.white70, fontSize: 10.0, fontFamily: "ukij-cjk");
    var gridHeaderTextStyle = gridTextStyle.copyWith(color: Colors.yellow, fontWeight: FontWeight.bold, fontSize: 12.0);
    var mainModel = ModelContainer.of<MainModel>(context).model;
    Widget buildingWidget = Scaffold(
      appBar: DolqunAppBar(),
      body: ListView(
        padding: EdgeInsets.all(10.0),
        children: <Widget>[
          SizedBox(height: 50.0,),
          Center(
            child: Image.asset("assets/images/DolqunVIP.png", width: 100.0,),
          ),
          SizedBox(height: 10.0,),
          Center(child: Text("VIP ئەزالىق ئىمتىياز كۆرسەتمىسى", style: TextStyle(color: Colors.yellow, fontSize: 15.0, fontWeight: FontWeight.bold),),),
          SizedBox(height: 20.0,),
          _buildPanel(
              child: Text("ئالاھىدە ئەزا بولغاندىن كېيىن كۆپىنچە فىلىملەرنى ھەقسىز چەكلىمىسىز كۆرەلەيسىز، بىر قىسىم پىلاندىن كۆپ ۋە بۇرۇن يوللانغان قىسىملىرىنى يېرىم باھادا سېتىۋېلىپ كۆرىسىز، ئالاھىدە ئەزالىق ۋاقتى توشقاندىن كېيىن ھەقسىز كۆرۈش ئىمتىيازىڭىز ئەمەلدىن قالىدۇ ھەمدە سېتىۋالغان فىلىملەرنى ھەرۋاقىت ھەقسىز كۆرەلەيسىز", textAlign: TextAlign.center,)
          ),
          SizedBox(height: 10.0,),
          _buildPanel(
              child: DataTable(
                horizontalMargin: 0.0,
                columnSpacing: 10.0,
                dataRowHeight: 60.0,
                columns: [
                  DataColumn(label: Text("ئەزالىق", style: gridHeaderTextStyle,)),
                  DataColumn(label: Text("ھەق ھالىتى", style: gridHeaderTextStyle,)),
                  DataColumn(label: Text("ئىزاھات", style: gridHeaderTextStyle,)),
                ],
                rows: [
                  DataRow(cells: [DataCell(Text("ئادەتتىكى ئەزا", style: gridTextStyle,)), DataCell(Text("ئەسلى باھاسى بويىچە سېتىلىدۇ", style: gridTextStyle,)), DataCell(Text("سېتىۋالغان فىلىم ھەر ۋاقىت ھەقسىز", style: gridTextStyle,))]),
                  DataRow(cells: [DataCell(Text("VIP ئەزا", style: gridTextStyle,)), DataCell(Text("VIP ئەزا بولغاندىن كىيىن كۆپ قىسمى ھەقسىز، سېتىۋالىدىغان فىلىم VIP باھا بويىچە ھىسابلىنىدۇ", style: gridTextStyle,)), DataCell(Text("VIP ئاخىرلاشقۇچە ھەقسىز، سېتىۋالغان فىلىملەر ھەقسىز", style: gridTextStyle,))]),
                ],
              )
          ),
          SizedBox(height: 10.0,),
          EnsureHasSignIn(
            builder: (wechatModel) => FutureBuilder<VipInformationResult>(
              future: mainModel.api.getVipListInformation(wechatModel.token),
              builder: (context, snapshot) {
                if (snapshot.hasError) return Center(child: Text(snapshot.error.toString(), style: TextStyle(color: Colors.white),),);
                if (snapshot.hasData) return Column(
                  mainAxisSize: MainAxisSize.min,
                  children: snapshot.data.data.vip.first.items.map((item) => _buildPriceItem(
                      tip: "ئەسلى باھاسى ${item.cost} يۈەن",
                      price: "${item.price} يۈەن / ${item.days} كۈن",
                      label: item.name,
                      onTap: () async {
                        BotToast.showLoading();
                        try {
                          var information = (await mainModel.api.requestPayment(PaymentFor.VIP, item.id, wechatModel.token)).data;
                          await wechatModel.payment(information);
                        }
                        catch (error) {
                          showCupertinoModalPopup(context: context, builder: (context) => CupertinoAlertDialog(
                            title: Text("An error occurred"),
                            content: Text(error.toString()),
                            actions: <Widget>[
                              CupertinoButton(
                                child: Text("چۈشىنەرلىك"),
                                onPressed: (){
                                  Navigator.of(context).pop();
                                },
                              )
                            ],
                          ));
//                          BotToast.showNotification(title: (canceller) => Text("An error occurred"), subtitle: (canceller) => Text(error.toString()));
                        }
                        BotToast.closeAllLoading();
                      }
                  )).toList(),
                );
                return Center(child: CircularProgressIndicator(),);
              },
            ),
          ),
        ],
      ),
    );
    buildingWidget = Directionality(
      textDirection: TextDirection.rtl,
      child: buildingWidget,
    );
    return buildingWidget;
  }
  Widget _buildPriceItem({String label, String price, String tip, void onTap()}) {
    return _buildPanel(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            Container(
              width: double.infinity,
              child: Text(label, textAlign: TextAlign.start, style: TextStyle(color: Colors.yellow, fontWeight: FontWeight.bold),),
            ),
            SizedBox(height: 5.0),
            Row(
              children: <Widget>[
                RaisedButton(
                  color: Colors.blue,
                  textColor: Colors.white,
                  child: Text("VIP بولاي"),
                  onPressed: onTap,
                ),
                SizedBox(width: 10.0,),
                Text(price),
                SizedBox(width: 5.0,),
                Text(tip, style: TextStyle(fontSize: 10.0, color: Colors.white38),)
              ],
            ),
          ],
        )
    );
  }
  Widget _buildPanel({Widget child}){
    return Container(
      child: child,
      margin: EdgeInsets.only(top: 10.0),
      decoration: BoxDecoration(
        color: Colors.white.withOpacity(0.04),
        borderRadius: BorderRadius.circular(14.0)
      ),
      padding: EdgeInsets.all(10.0),
    );
  }
}